import axios from "axios";

/**
 * 网络请求配置
 */
//axios.defaults.timeout = 100000;
// 本地mock 测试
axios.defaults.baseURL = "/custom_api";


/**
 * 请求拦截器
 */
axios.interceptors.request.use(
    config => {
        return config;
    },
    error => {
        return Promise.reject(error);
    }
);
/**
 * 响应拦截器
 */
axios.interceptors.response.use(
    response => {
        return response;
    },
    error => {
        return Promise.reject(error);
    }
);

/**
 * 封装get方法
 * @param url  请求url
 * @param params  请求参数
 * @returns {Promise}
 */
function get(url, params = {}) {
    return new Promise((resolve, reject) => {
        axios.get(url, {
            params: params,
        }).then((response) => {
            resolve(response.data);
        })
            .catch((error) => {
                reject(error);
            });
    });
}

/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */

function post(url, data) {
    return new Promise((resolve, reject) => {
        axios.post(url, data).then(
            (response) => {
                //关闭进度条
                resolve(response.data);
            },
            (err) => {
                reject(err);
            }
        );
    });
}

function all(all) {
    return axios.all(all);
}

const http = axios.create()
http.doGet = get;
http.doPost = post;
http.doAll = all;
export default http
